﻿using BCCommon;
using BCCommon.TMS.Invoice;
using BCData.TMS.User.UserInvoiceThin;
using BCDto.TMS.User.UserInvoiceThin;
using BCDto.Website.User;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.User.UserInvoiceThin;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.User.UserInvoiceThin
{
    public class UserInvoiceThinService : IUserInvoiceThinService
    {
        private readonly IUserInvoiceThinData userInvoiceThinData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.UserInvoiceThinEntity);

        public UserInvoiceThinService(IUserInvoiceThinData userInvoiceThinData, 
            IRedisService redisService)
        {
            this.userInvoiceThinData = userInvoiceThinData;
            this.redisService = redisService;
        }

        public UserInvoiceThinDto Add(UserDto userInfo, UserInvoiceThinRequestDto dto)
        {
            var result = this.userInvoiceThinData.Insert(new UserInvoiceThinEntity()
            {
                UserId = userInfo.UserId,
                InvoiceTitleType = dto.InvoiceTitleType.ToString(),
                CompanyName = dto.CompanyName,
                BusinessLicenseNo = dto.BusinessLicenseNo,
                CompanyFinanceNo = dto.CompanyFinanceNo,
                CompanyPhone = dto.CompanyPhone,
                CompanyAddress = dto.CompanyAddress,
                BankName = dto.BankName,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now
            }).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<UserInvoiceThinDto>();
        }

        public void Delete(UserDto userInfo, long userInvoiceThinId)
        {
            var result = this.userInvoiceThinData.GetEntity(userInvoiceThinId).Result;
            if (result == null)
            {
                throw new ArgumentException("发票薄信息不存在");
            }
            if (result.UserId != userInfo.UserId)
            {
                throw new ArgumentException("此发票薄不属于您，暂无权限删除");
            }
            this.userInvoiceThinData.Delete(userInvoiceThinId).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
        }

        public UserInvoiceThinDto GetById(long userInvoiceThinId)
        {
            string cacheKey = this.redisService.GetKey(key, userInvoiceThinId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.userInvoiceThinData.GetEntity(userInvoiceThinId).Result;
                return data.As<UserInvoiceThinDto>();
            }, TimeSpan.FromDays(1));
            return result;
        }

        public IEnumerable<UserInvoiceThinDto> GetList(UserDto userInfo, InvoiceTitleType? invoiceTitleType)
        {
            string cacheKey = this.redisService.GetKey(key, userInfo.UserId, invoiceTitleType);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.userInvoiceThinData.GetList(userInfo.UserId, invoiceTitleType).Result;
                return data.As<List<UserInvoiceThinDto>>();
            }, TimeSpan.FromDays(1));
            return result;
        }

        public PageableList<UserInvoiceThinDto> Query(UserDto userInfo, InvoiceTitleType? invoiceTitleType, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, userInfo.UserId, invoiceTitleType, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var (data, dataCount) = this.userInvoiceThinData.Query(userInfo.UserId, invoiceTitleType, pageIndex, pageSize).Result;
                return new PageableList<UserInvoiceThinDto>()
                {
                    Count = dataCount,
                    Items = data.As<List<UserInvoiceThinDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize
                };
            }, TimeSpan.FromDays(1));
            return result;
        }

        public UserInvoiceThinDto Update(UserDto userInfo, long userInvoiceThinId, UserInvoiceThinRequestDto dto)
        {
            var result = this.userInvoiceThinData.GetEntity(userInvoiceThinId).Result;
            if (result == null)
            {
                throw new ArgumentException("发票薄信息不存在");
            }
            if (result.UserId != userInfo.UserId)
            {
                throw new ArgumentException("此发票薄不属于您，暂无权限修改");
            }
            result.InvoiceTitleType = dto.InvoiceTitleType.ToString();
            result.CompanyName = dto.CompanyName;
            result.BusinessLicenseNo = dto.BusinessLicenseNo;
            result.CompanyFinanceNo = dto.CompanyFinanceNo;
            result.CompanyPhone = dto.CompanyPhone;
            result.CompanyAddress = dto.CompanyAddress;
            result.BankName = dto.BankName;
            result.UpdateTime = DateTime.Now;
            result = this.userInvoiceThinData.Update(result).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<UserInvoiceThinDto>();
        }
    }
}
